package student.service;

import student.dto.BookDto;
import student.utils.FileUtil;
import student.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;


public class BookService {
    /**
     *  删除一本书
     */
    public void deleteById(Integer id) {
        //过滤掉 需要删除的id
        Main.bookDtos = Main.bookDtos.stream().filter(st -> !Objects.equals(st.getId(), id)).collect(Collectors.toList());
        this.delete();
    }

    private void delete(){
        FileUtil fileUtil = new FileUtil();
        fileUtil.deleteFile(Main.bookFilePath);
        List<String> strings = getStrings();
        try {
            fileUtil.write(Main.bookFilePath, strings);
        } catch (IOException e) {
            System.out.println("删除失败");
            e.printStackTrace();
        }
    }

    private List<String> getStrings() {
        List<String> strings = new ArrayList<>();
        strings.add("ID,书名,书页数,学生ID");
        for (int i = 0; i < Main.bookDtos.size(); i++) {
            BookDto bookDto = Main.bookDtos.get(i);
            int index = i+1;

            strings.add(index+","+bookDto.getName()+","+bookDto.getNum()+","+bookDto.getStudentId());
        }

        return strings;
    }


    /**
     * 修改书籍名称
     */
    public void updateName() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(Main.bookFilePath);
        System.out.println("----------请输入 需要修改书籍的Id---------");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        BookDto bookDto = Main.bookDtos.get(id - 1);

        System.out.println("ID,书名,页数,学生ID");
        System.out.println(bookDto.getId() + "," + bookDto.getName() + "," + bookDto.getNum() + "," + bookDto.getStudentId());
        System.out.println("----------请输入 需要修改后的书籍名称---------");
        String name = sc.next();
        Main.bookDtos.get(id - 1).setName(name);

        this.delete();
        System.out.println("----------修改成功  ---------");
        fileUtil.queryAll(Main.bookFilePath);
    }


    /**
     * 添加 指定位置添加一本书
     */
    public void add() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(Main.bookFilePath);
        System.out.println("----------请输入 需要插入的位置Id---------");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        BookDto bookDto = new BookDto();
        System.out.println("----------请输入 书本名称---------");
        bookDto.setName(sc.next());
        System.out.println("----------请输入 书本页数---------");
        bookDto.setNum(sc.next());
        fileUtil.queryAll(Main.studentFilePath);
        System.out.println("----------请输入 需要绑定的学生ID---------");
        bookDto.setStudentId(sc.nextInt());
        Main.bookDtos.add(id, bookDto);

        int i = 0;

        for (BookDto bookDto1 : Main.bookDtos) {
            bookDto1.setId(i);
            i++;
        }
        this.delete();
        System.out.println("----------添加成功---------");
        fileUtil.queryAll(Main.bookFilePath);
    }
}
